package checkDetail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import weaver.conn.RecordSet;
import weaver.general.Util;

import general.BaseBean;

public class WorkflowDocCatalogueCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("WorkflowDocCatalogueCheck checkData start");
        //定义查询sql
        String checkSql = "select id as workflowid, workflowname, doccategory, docpath from workflow_base where doccategory is not null and doccategory <> ',,'";
        recordSet.executeSql(checkSql);
        List resultList = new ArrayList();
        String result = "";
        
        //重新实例化一个rs
        RecordSet rs1 = new RecordSet();
        while(recordSet.next()){
            //根据sql返回值doccategory来查询目录是否存在
            String workflowId = recordSet.getString("workflowid");
            String workflowName = recordSet.getString("workflowname");
            String docCategoryS = recordSet.getString("doccategory");
            
            if(docCategoryS.contains(",")){
                String docCategoryId = docCategoryS.substring(docCategoryS.lastIndexOf(",") + 1, docCategoryS.length());
                rs1.executeSql("select categoryname, parentid  from DocSecCategory where id = " + docCategoryId);
                if(!rs1.next())
                {
                    Map resultMap = new HashMap<String, String>();
                    resultMap.put("showMessage", formatMessage(workflowName, "路径中附件上传目录异常。因为该目录在内容引擎中被删除，导致路径中附件上传目录关联不到对应数据，请先重新设置附件上传目录，再点击修复！"));
                    resultMap.put("linkurl","/workflow/workflow/addwf0.jsp?ajax=1&src=editwf&wfid=" + workflowId + "&isTemplate=0");
                    resultMap.put("linkname","点击此处进行路径上传附件目录设置。");
                    resultMap.put("buttonname","修复");
                    resultMap.put("workflowid",workflowId);
                    resultList.add(resultMap);
                }
            }else{
                Map resultMap = new HashMap<String, String>();
                resultMap.put("showMessage", formatMessage(workflowName, "路径中附件上传目录数据异常,目录结构缺失，请先重新设置附件上传目录，再点击修复！"));
                resultMap.put("linkurl","/workflow/workflow/addwf0.jsp?ajax=1&src=editwf&wfid=" + workflowId + "&isTemplate=0");
                resultMap.put("linkname","点击此处进行路径上传附件目录设置。");
                resultMap.put("buttonname","修复");
                resultMap.put("workflowid",workflowId);
                resultList.add(resultMap);
            }
        }
        if(resultList.size() == 0){
            result = resultOk;
            resultList.add("路径设置中路径上传附件目录没有异常。");
        }else{
            result = resultNeedFix;
        }
        
        (new BaseBean()).writeLog("WorkflowDocCatalogueCheck checkData end");
        return formatReturnJsonCheck(result, resultList, this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("WorkflowDocCatalogueCheck fixData start");
        String divid = (String)dataMap.get("divid");
        if(dataMap.containsKey("workflowid") && !"".equals(dataMap.get("workflowid"))){
            //根据参数获取列表
            String workflowids = (String)dataMap.get("workflowid");
            List<String> workflowidList = Util.splitString2List(workflowids, ",");
            //如果点击是的一键修复则不修复，只能单个修复，修复前先检测路径附件上传目录是否存在
            if(workflowidList.size() == 1)
            {
                List resultList = new ArrayList();
                Map resultMap = new HashMap<String,String>();
                String result = resultOk;
                //根据workflowid查询doccategory在文档目录中是否存在
                recordSet.executeSql("select id as workflowid, workflowname, doccategory, formid from workflow_base where id = " + workflowids);
                recordSet.next();
                String docCategoryS = recordSet.getString("doccategory");
                String formid = recordSet.getString("formid");
                String docCategoryId = docCategoryS.substring(docCategoryS.lastIndexOf(",") + 1, docCategoryS.length());
                //重新实例化一个rs
                RecordSet rs1 = new RecordSet();
                RecordSet rs2 = new RecordSet();
                RecordSet upDocRs = new RecordSet();
                rs1.executeSql("select categoryname, parentid  from DocSecCategory where id = " + docCategoryId);
                //重新设置路径附件上传目录后,需要修复 重新设置路径上传附件目录前已经生成的附件文档  对应目录
                if(rs1.next()){
                    //根据表单表单id查询附件字段,并拼接合并查询sql
                    StringBuffer docFieldSqlBuff = new StringBuffer();
                    String docFieldSql = "";
                    
                    rs2.executeSql("select fieldname from workflow_billfield where fieldhtmltype = 6 and billid = " + formid);
                    while(rs2.next()){
                        docFieldSqlBuff.append("select " + rs2.getString("fieldname") + " as docField from formtable_main_" + formid.replace("-", ""));
                        docFieldSqlBuff.append(" union ");
                    }
                    
                    if(!"".equals(docFieldSqlBuff.toString())){
                        docFieldSql = docFieldSqlBuff.toString();
                        docFieldSql = docFieldSql.substring(0, docFieldSql.length() - 7);
                    }
                    
                    //文档Id的集合
                    List docIdList = new ArrayList();
                    //执行合并查询sql在表单formtable_main_中查询附件字段的值
                    rs2.executeSql(docFieldSql);
                    while(rs2.next()){
                        String docIdS = rs2.getString("docField");
                        if(StringUtils.isNotEmpty(docIdS)){
                            if(docIdS.contains(",")){
                                String[] docIdArr = docIdS.split(",");
                                for (String docId : docIdArr) {
                                    docIdList.add(docId);
                                }
                            }else{
                                docIdList.add(docIdS);
                            }
                        }
                    }
                    
                    //如果文档Id的集合不为空，则修改文档中seccategory为新设置的docCategoryId
                    if(docIdList.size() > 0){
                      //文档Id集合转换成in的查询条件
                        String docIdListStr = docIdList.toString().replace("[", "(").replace("]", ")");
                        upDocRs.executeUpdate("update docdetail a set a.seccategory = " + docCategoryId + " where not exists(select 1 from DocSecCategory b where a.seccategory = b.id) and id in " + docIdListStr);
                        resultMap.put("result",resultOk);
                        resultMap.put("fixMessage","修复成功");
                        resultMap.put("divid",divid);
                    }else{
                        resultMap.put("result",resultOk);
                        resultMap.put("fixMessage","修复成功");
                        resultMap.put("divid",divid);
                    }
                   
                }else{
                    result = resultNeedFix;
                    resultMap.put("result",resultNeedFix);
                    resultMap.put("fixMessage","修复失败，请先重新设置路径中附件上传目录！");
                    resultMap.put("divid",divid);
                }
                resultList.add(resultMap);
                return formatReturnJsonFix(result, resultList,this);
            }
        }
        return checkData(new HashMap());
    }

    @Override
    public String getClassDesc() {
        return "检测路径设置中路径上传附件目录，在文档目录下是否被删除的问题";
    }

    @Override
    public String getClassName() {
        return "路径设置中路径上传附件目录检测";
    }

}
